﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

public class RandomBoxSelectWindow : MonoBehaviour {
	public RewardCard[] boxItems;
	public RewardCard[] boxItems2;
	public MyUnitCard[] cardObject;

	List<int> selectedBoxIndex = new List<int>();

	public UILabel chooseCountLabel;

	public GameObject[] buttons;

	public UISlider remainTimeGage;

	// Use this for initialization
	void Start () {

//		SetWindowInfos(null, null, null, 3, 1);
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	int _chooseCount = 0;
	int[] _boxCategoryList;
	public void SetWindowInfos(int[] boxCategoryList, int[] boxCIdList, int[] amountList, int boxCount, int chooseCount)
	{
		_boxCategoryList = boxCategoryList;
		_chooseCount = chooseCount;
//		chooseCountLabel.text = "0 / "+chooseCount;

		int i=0;
        //TweenAlpha[] twAlpha = new TweenAlpha[3];
		for(i=0; i<boxCount; i++)
		{
			if( boxCategoryList[i] == (int)BoxCategory.COIN )
			{
				boxItems[i].SetItemInfos((BoxCategory)boxCategoryList[i], boxCIdList[i], amountList[i]);
				boxItems2[i].SetItemInfos((BoxCategory)boxCategoryList[i], boxCIdList[i], amountList[i]);
//				boxItems[i].SetItemInfos((BoxCategory)boxCategoryList[i], boxCIdList[i], amountList[i]);
//				boxItems[i].window = this;
//				boxItems[i].index = i;
//				boxItems[i].gameObject.SetActive(true);
//				twAlpha[i] = boxItems[i].gameObject.AddComponent<TweenAlpha>();
//				twAlpha[i].from = 0f; 
//				twAlpha[i].to = 1f;
//				twAlpha[i].delay = 2f;
//				twAlpha[i].duration = 3f;
//				EventDelegate.Set(twAlpha[i].onFinished, OnAlphaEffectFinished);
//				twAlpha[i].Play(true);
			}
			else if( boxCategoryList[i] == (int)BoxCategory.UNIT )
			{
				CUnitData unitData = CUnitDataManager.Instance.GetUnitData(boxCIdList[i]);
				CUnitAbilityItem plusAbility = CUnitAbilityDataManager.Instance.GetUnitAbilityData("1_" + unitData.unit_class);
				cardObject[i].SetCardInfo(i, 1, unitData, plusAbility);
				boxItems[i].itemImage.gameObject.SetActive(false);
				boxItems[i].itemAmount.gameObject.SetActive(false);
				boxItems2[i].itemImage.gameObject.SetActive(false);
				boxItems2[i].itemAmount.gameObject.SetActive(false);
//				cardObject[i].gameObject.SetActive(true);
//                boxItems[i].window = this;
//                boxItems[i].index = i;
//				boxItems[i].isEmpty = true;
//				TweenAlpha twAlpha2 = cardObject.gameObject.AddComponent<TweenAlpha>();
//				twAlpha2.from = 0f; 
//				twAlpha2.to = 1f;
//				twAlpha2.delay = 0.5f;
//				twAlpha2.duration = 5f;
//				twAlpha2.Play(true);
			}
		}
	}
	
	void OnAlphaEffectFinished()
	{
//		for(int i=0; i<3; i++)
//		{
//			if( UITweener.current.gameObject == boxItems[i].gameObject ) 
//			{
//				TweenPosition twPosition = boxItems[i].gameObject.AddComponent<TweenPosition>();
//				twPosition.from = boxItems[i].transform.localPosition;
//				twPosition.to = Vector3.zero;
//				twPosition.delay = 0f;
//				twPosition.duration = 1.5f;
//				EventDelegate.Set(twPosition.onFinished, OnAlphaEffectFinished);
//				twPosition.Play(true);
//			}
//		}
	}

	int selectedBoxCount = 0;
	public void SelecteBox(int index, int boxIndex, GameObject seleecteMark)
	{
//		if( _boxCategoryList[index] == (int)BoxCategory.UNIT )
//			boxItems[index].gameObject.SetActive(false);

		if( selectedBoxCount < _chooseCount ) 
		{
			selectedBoxCount++;
//			chooseCountLabel.text = selectedBoxCount + " / " + _chooseCount;
			boxItems[index].GetComponent<Animator>().enabled = true;
			SoundManager.Instance.PlaySound(ESound.RewardGacha_select);
//			if( !boxItem.isEmpty )
//			{
//				boxItem.itemName.gameObject.SetActive(true);
//				boxItem.itemAmount.gameObject.SetActive(true);
//				boxItem.itemImage.gameObject.SetActive(true);
//				boxItem.questionMark.gameObject.SetActive(false);
//			}

			if( seleecteMark != null )
				seleecteMark.SetActive(true);

			if( isOneMoreBox )
				CSceneManager.manager.SendBuyOneMoreRandomBox(boxIndex);
			else
				CSceneManager.manager.SendSelectRandomBox(boxIndex);
		}

		if( selectedBoxCount == _chooseCount )
		{
			if( selectedBoxCount < 3 )
			{
				foreach(GameObject obj in buttons)
					obj.SetActive(true);
			}
			else if( selectedBoxCount == 3 )
			{
				buttons[0].SetActive(false);
				buttons[1].SetActive(true);
			}
		}
	}
	
	bool isOneMoreBox = false;
	void OnClickedOnMoreRandomBoxButton()
	{
		if( MyInfo.Instance.Diamond >= 1 )
		{
			_chooseCount++;
//			chooseCountLabel.text = selectedBoxCount + " / " + _chooseCount;
			isOneMoreBox = true;
		}
		else
		{
			UIManager.Instance.ShowNotEnoughDiamondWindow(false);
		}

		foreach(GameObject obj in buttons)
			obj.SetActive(false);
	}

	void OnClickedExit()
	{
		gameObject.SetActive(false);
//		StageManagerWrapper.SendLeave();
	}

	public BoxCategory GetBoxCategoryByIndex(int index)
	{
		return (BoxCategory)_boxCategoryList[index];
	}

	public void ShowUnitCard(int index)
	{
		if( (BoxCategory)_boxCategoryList[index] == BoxCategory.UNIT )
		{
			cardObject[index].gameObject.SetActive(true);
		}
		else
		{
			boxItems[index].itemImage.gameObject.SetActive(true);
			boxItems[index].itemAmount.gameObject.SetActive(true);
		}
	}	
}
